package com.lyk.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.common.dao.BaseDao;
import com.common.entity.A_adminer;
import com.common.entity.PageBean;
import com.lyk.dao.A_adminerDao;

public class A_adminerDaoImpl extends BaseDao<A_adminer> implements A_adminerDao {

	@Override
	public List<A_adminer> findByCondition(Map<String, Object> mp) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_adminer where 1=1 ");
		if(mp!=null && mp.size()>0){
			if(mp.get("adminer_name")!=null){
				sf.append("and admin_name like ? ");
				paramList.add(mp.get("adminer_name"));
			}
			if(mp.get("adminer_time")!=null){
				sf.append("and to_char(createTime,'yyyy-MM-dd')=?");
				paramList.add(mp.get("adminer_time"));
			}
		}
		return findBySql(sf.toString(), paramList.toArray());
	}

	@Override
	public int delA_adminer(Integer n) {
		String sql = "delete a_adminer where admin_id = ?";
		return this.executeUpdate(sql, new Object[]{n});
	}

	@Override
	public A_adminer findById(Integer n) {
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer sf=new StringBuffer();
		sf.append(" select * from a_adminer where admin_id = ? ");
		paramList.add(n);
		List<A_adminer> list = findBySql(sf.toString(), paramList.toArray());
		return (list!=null&&list.size()>0)?list.get(0):null;
	}

	@Override
	public int changeEmp(A_adminer adminer) {
		String sql = "update a_adminer set admin_name=?,admin_pwd=?,realName=?,createTime=?,telephone=?,is_login=? where admin_id = ?";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(adminer.getAdmin_name());
		paramList.add(adminer.getAdmin_pwd());
		paramList.add(adminer.getRealName());
		paramList.add(adminer.getCreateTime());
		paramList.add(adminer.getTelephone());
		paramList.add(adminer.getAdmin_id());
		return this.executeUpdate(sql, paramList.toArray());
	}

	@Override
	public int saveEmp(A_adminer adminer) {
		String sql="insert into a_adminer(admin_id,admin_name,admin_pwd,realName,createTime,telephone) values(seq_a_adminer.nextval,?,?,?,sysdate,?)";
		List<Object> paramList=new ArrayList<Object>();
		paramList.add(adminer.getAdmin_name());
		paramList.add(adminer.getAdmin_pwd());
		paramList.add(adminer.getRealName());
		paramList.add(adminer.getTelephone());
		return this.executeUpdate(sql, paramList.toArray());
	}
	public A_adminer getEntity(ResultSet rs){
		try{
			A_adminer adminer = new A_adminer();
			adminer.setAdmin_id(rs.getInt("admin_id"));
			adminer.setAdmin_name(rs.getString("admin_name"));
			adminer.setAdmin_pwd(rs.getString("admin_pwd"));
			adminer.setRealName(rs.getString("realName"));
			adminer.setCreateTime(rs.getDate("createTime"));
			adminer.setTelephone(rs.getString("telephone"));	
			return adminer;
		} catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public PageBean<A_adminer> findPageByCondition(int pageIndex, int pageSize,
			Map<String, Object> mp) {
		PageBean<A_adminer> pageBean=new PageBean<A_adminer>();
		pageBean.setPageIndex(pageIndex);
		pageBean.setPageSize(pageSize);
		List<Object> paramList=new ArrayList<Object>();
		StringBuffer querySql=new StringBuffer();
		querySql.append(" select * from a_adminer where 1=1");
		if(mp!=null && mp.size()>0){
			if(mp.get("adminer_name")!=null){
				querySql.append("and admin_name like ? ");
				paramList.add(mp.get("adminer_name"));
			}
			if(mp.get("adminer_time")!=null){
				querySql.append("and to_char(createTime,'yyyy-MM-dd')=?");
				paramList.add(mp.get("adminer_time"));
			}
		}
		pageBean.setTotalCount(this.getCount(querySql.toString(), paramList.toArray()));
		if(pageBean.getTotalCount()>0){
			List<A_adminer> list = findPage(pageBean, paramList, querySql);
			pageBean.setList(list);
		}
		
		return pageBean;
	}
	
	private List<A_adminer> findPage(PageBean<A_adminer> pageBean,
			List<Object> paramList, StringBuffer querySql) {
		List<A_adminer> list=new ArrayList<A_adminer>();
		StringBuffer queryPageSql=new StringBuffer();
		queryPageSql.append(" select * from (");
		queryPageSql.append("  select tmp.*,rownum rn from ( ");
		queryPageSql.append(querySql.toString());
		queryPageSql.append("  ) tmp ");
		queryPageSql.append(" ) where rn>? and rn<=? ");
		int startRow=(pageBean.getPageIndex()-1)*pageBean.getPageSize();
		int endRow=pageBean.getPageIndex()*pageBean.getPageSize();
		paramList.add(startRow);
		paramList.add(endRow);
		Connection conn=this.getConnection();
		PreparedStatement prs=null;
		ResultSet rs=null;
		try {
			conn=this.getConnection();
			prs=conn.prepareStatement(queryPageSql.toString());
			Object[] objs=paramList.toArray();
			if(objs!=null && objs.length>0){
				for(int i=0;i<objs.length;i++){
					prs.setObject(i+1, objs[i]);
				}
			}
			rs=prs.executeQuery();
			
			while(rs.next()){
				A_adminer adminer = new A_adminer();
				adminer.setAdmin_id(rs.getInt("admin_id"));
				adminer.setAdmin_name(rs.getString("admin_name"));
				adminer.setAdmin_pwd(rs.getString("admin_pwd"));
				adminer.setRealName(rs.getString("realName"));
				adminer.setCreateTime(rs.getDate("createTime"));
				adminer.setTelephone(rs.getString("telephone"));
				list.add(adminer);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
}
